<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2012.03.17.
   家居商家在建工地控制器($小牛New $2014.07.28)*/

!defined('Q_PATH') && exit;

class JiajubuildController extends AController{

	public function init__(){
		$this->_arrNotMethod=array(
			'save_previewstatus',
			'save_preview',
			'view_preview',
			'foreverdelete_deep',
			'input_change_ajax',
			'input_change_unique',
			'forbid',
			'resume',
			'clear_recycle',
			'save_sort'
		);
		
		parent::init__();
	}
	
	public function filter_(&$arrMap){
		$arrMap['A.jiajubuild_title']=array('like',"%".Q::G('jiajubuild_title')."%");
		$arrMap['A.jiajubuild_owner']=array('like',"%".Q::G('jiajubuild_owner')."%");
		$arrMap['A.jiajubuild_viewnum']=array('egt',intval(Q::G('jiajubuild_viewnum')));
		$arrMap['A.jiajubuild_realprice']=array('egt',intval(Q::G('jiajucase_realprice')));
		$arrMap['A.jiajubuild_realspace']=array('egt',intval(Q::G('jiajucase_realspace')));
		$arrMap['A.jiajucompany_id']=$GLOBALS['_company_']['jiajucompany_id'];
		
		// 添加时间
		$this->getTime_('A.create_dateline',$arrMap);
	}

	public function bAdd_(){
		// 工地属性
		$oJiajucompanyclass=Q::instance('JiajucompanyclassModel');
		$this->assign('arrStyleclass',$oJiajucompanyclass->getClass(52));
		$this->assign('arrSpaceclass',$oJiajucompanyclass->getClass(53));
		$this->assign('arrPriceclass',$oJiajucompanyclass->getClass(50));
		$this->assign('arrAreaclass',$oJiajucompanyclass->getClass(51));

		// 设计师
		$arrDesignerids=Model::F_('jiajudesigner','jiajucompany_id=? AND jiajudesigner_status=1',$GLOBALS['_company_']['jiajucompany_id'])
			->setColumns('jiajudesigner_id,jiajudesigner_name')
			->getAll();
		$this->assign('arrDesignerids',$arrDesignerids);
	}

	public function bEdit_(){
		$this->bAdd_();
	}

	protected function AEditObject_($oModel){
		if(in_array($oModel->jiajubuild_status,array(9,3)) || $oModel->jiajucompany_id!=$GLOBALS['_company_']['jiajucompany_id']){
			$this->E('你没有权限访问。');
		}
	}

	protected function AInsertObject_($oModel){
		// 商家&城市
		$oModel->jiajucompany_id=$GLOBALS['_company_']['jiajucompany_id'];
		$oModel->site_id=$GLOBALS['_company_']['site_id'];

		// 添加人
		$oModel->user_id=$GLOBALS['___login___']['user_id'];
		$oModel->jiajubuild_username=$GLOBALS['___login___']['user_name'];
		$oModel->jiajubuild_editusername=$GLOBALS['___login___']['user_name'];
		$oModel->jiajubuild_edituserid=$GLOBALS['___login___']['user_id'];
	}

	protected function AUpdateObject_($oModel){
		// 更新人
		$oModel->jiajubuild_editusername=$GLOBALS['___login___']['user_name'];
		$oModel->jiajubuild_edituserid=$GLOBALS['___login___']['user_id'];

		// 修改后提交
		if($oModel->jiajubuild_status==1){
			$oModel->jiajubuild_status=11;
		}
	}

	public function diary(){
		$nId=intval(Q::G('id','G'));
		if(empty($nId)){
			$this->E('未指定在建工地');
		}

		$arrBuild=Model::F_('jiajubuild','jiajubuild_id=? AND jiajucompany_id=?',$nId,$GLOBALS['_company_']['jiajucompany_id'])
			->setColumns('jiajubuild_id,jiajubuild_title')
			->getOne();
		if(empty($arrBuild['jiajubuild_id'])){
			$this->E('该在建工地不存在');
		}

		$arrDiarys=Model::F_('jiajubuilddiary','jiajubuild_id=?',$nId)
			->order('jiajubuilddiary_progress DESC')
			->getAll();

		$this->assign('arrDiarys',$arrDiarys);
		$this->assign('arrBuild',$arrBuild);
		$this->display();
	}

	public function adddiary(){
		$nBid=intval(Q::G('bid'));
		if(empty($nBid)){
			$this->E('未指定在建工地');
		}

		$arrBuild=Model::F_('jiajubuild','jiajubuild_id=? AND jiajucompany_id=?',$nBid,$GLOBALS['_company_']['jiajucompany_id'])
			->getOne();
		if(empty($arrBuild['jiajubuild_id'])){
			$this->E('该在建工地不存在');
		}

		if($arrBuild['jiajubuild_progress']==7){
			$this->E('该在建工地已经施工结束，无法再添加工程进度。');
		}

		// 保存
		if($this->isPost()){
			$oDiary=new JiajubuilddiaryModel();
			$oDiary->jiajubuild_id=$nBid;
			$oDiary->jiajucompany_id=$GLOBALS['_company_']['jiajucompany_id'];
			$oDiary->save();
			if($oDiary->isError()){
				$this->E($oDiary->getErrorMessage());
			}else{
				Model::M_('jiajubuild')->updateWhere(array('jiajubuild_progress'=>intval(Q::G('jiajubuilddiary_progress'))),'jiajubuild_id=?',$nBid);
				
				$this->A($oDiary->toArray(),'施工记录添加成功',1);
			}
		}

		$this->assign('arrBuild',$arrBuild);
		$this->assign('nBid',$nBid);
		$this->display();
	}

	public function editdiary(){
		$nBid=intval(Q::G('bid'));
		$nId=intval(Q::G('id'));
		if(empty($nBid)){
			$this->E('未指定在建工地');
		}

		$arrBuild=Model::F_('jiajubuild','jiajubuild_id=? AND jiajucompany_id=?',$nBid,$GLOBALS['_company_']['jiajucompany_id'])
			->getOne();
		if(empty($arrBuild['jiajubuild_id'])){
			$this->E('该在建工地不存在');
		}

		$oDiary=JiajubuilddiaryModel::F('jiajubuild_id=? AND jiajubuilddiary_id=?',$nBid,$nId)->getOne();
		if(empty($oDiary['jiajubuilddiary_id'])){
			$this->E('该施工记录不存在');
		}

		// 保存
		if($this->isPost()){
			$oDiary->save('update');
			if($oDiary->isError()){
				$this->E($oDiary->getErrorMessage());
			}else{
				$this->S('施工记录更新成功');
			}
		}

		$this->assign('arrBuild',$arrBuild);
		$this->assign('oValue',$oDiary);
		$this->assign('nBid',$nBid);
		$this->assign('nId',$nId);
		$this->display('jiajubuild/adddiary');
	}

	public function order(){
		$nId=intval(Q::G('id','G'));
		if(empty($nId)){
			$this->E('未指定在建工地');
		}

		$arrBuild=Model::F_('jiajubuild','jiajubuild_id=? AND jiajucompany_id=?',$nId,$GLOBALS['_company_']['jiajucompany_id'])
			->setColumns('jiajubuild_id,jiajubuild_title')
			->getOne();
		if(empty($arrBuild['jiajubuild_id'])){
			$this->E('该在建工地不存在');
		}

		$arrOrders=Model::F_('jiajubuildorder','jiajubuild_id=? AND jiajubuildorder_status != 9',$nId)
			->order('jiajubuildorder_id DESC')
			->getAll();

		$this->assign('arrOrders',$arrOrders);
		$this->assign('arrBuild',$arrBuild);
		$this->display();
	}

	public function set_status(){
		$nId=intval(Q::G('id'));
		$nStatus=intval(Q::G('status'));
		if(empty($nId)){
			$this->E('未指定预约参观订单');
		}
		if(!in_array($nStatus,array(1,13,9))){
			$this->E('预约参观订单状态值错误');
		}
		
		$oOrder=JiajubuildorderModel::F('jiajubuildorder_id=? AND jiajucompany_id=?',$nId,$GLOBALS['_company_']['jiajucompany_id'])->getOne();
		if(empty($oOrder)){
			$this->E('预约参观订单不存在');
		}

		$oOrder->jiajubuildorder_status=$nStatus;
		$oOrder->save('update');
		if($oOrder->isError()){
			$this->E($oOrder->getErrorMessage());
		}else{
			$this->S('操作成功');
		}
	}

	public function orderStatus($status){
		switch($status){
			case 0:// 客户下单
				return '<span style="color:blue;">等待确认</span>';
				break;
			case 1:// 商家确认
				return '<span style="color:#0DD07F;">商家已确认</span>';
				break;
			case 13:
				return '<span style="color:gray;">订单关闭</span>';
				break;
			default:
				return '未知';
				break;
		}
	}

}
